package com.njust.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.njust.common.Const;
import com.njust.common.ServerRespone;
import com.njust.common.TokenCache;
import com.njust.dataobject.User;
import com.njust.service.UserService;
import com.njust.vo.UserVO.PasswordVO;
import com.njust.vo.UserVO.UserVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/user")
@Api("用户模块")
@Slf4j
public class UserContronller {

    @Autowired
    private UserService userService;
    
    /**
     * 登入
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value="/login", notes="登入")
    public ServerRespone login(@RequestBody User user, HttpSession session, HttpServletResponse response){
        UserVO userVO=new UserVO();
        User u = userService.login(user);
        if(u==null){
            return ServerRespone.createByErrorMessage("用户名或者密码错误");
        }
        BeanUtils.copyProperties(u,userVO);
        Map<String,Object> map=new HashMap<>();
        //生成token
        String Token= UUID.randomUUID().toString();
        //存放到缓存中
        TokenCache.setKey(TokenCache.TOKEN_PREFIX+u.getUsername(),Token);
        log.info("token_key={}",TokenCache.TOKEN_PREFIX+u.getUsername());
        log.info("token_value={}",TokenCache.getKey(TokenCache.TOKEN_PREFIX+u.getUsername()));
        map.put("user",userVO);
        map.put("token",Token);
        //存放登入信息到session中
        session.setAttribute(Const.CURRENT_USER,u.getUsername());
        response.addHeader("token",Token);
        return ServerRespone.createBySuccess("登入成功",map);
    }
    /**
     * 登出用户
     * @param session
     */
    @GetMapping("/logout")
    @ApiOperation(value="/logout", notes="登出")
    public ServerRespone logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerRespone.createBySuccessMessage("登出成功");
    }

    /**
     * 注册或添加
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value="/register", notes="注册")
    public ServerRespone register(@RequestBody User user){
        return userService.save(user);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT,consumes = "application/json")
    @ApiOperation(value="/update", notes="更新用户")
    public ServerRespone update(@RequestBody User user){
        ServerRespone ServerRespone = userService.update(user);
        return ServerRespone;
    }

    /**
     * 修改密码
     * @param passwordVO
     * @return
     */
    @PostMapping("/password")
    @ApiOperation(value="/password", notes="修改密码")
    public ServerRespone modify(@RequestBody PasswordVO passwordVO){
        ServerRespone ServerRespone = userService.modifyPassword(passwordVO);
        return ServerRespone;
    }

    @GetMapping("/allusers")
    @ApiOperation(value="/allusers", notes="管理员查询所有用户")
    public ServerRespone findAllUser(){
        ServerRespone ServerRespone = userService.findAllUsers();
        return ServerRespone;
    }
}
